package com.umeox.watch.moto.chat.utils;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;

import androidx.core.content.res.ResourcesCompat;

import com.umeox.moto.common.decoration.VerticalImageSpan;
import com.umeox.watch.moto.chat.bean.EmojiBean;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmoticonConstants {


    private static final Pattern EMOTICON = Pattern.compile("[0-9]{3},");
    public static final int EMOJI_COUNT = getEmojiCount();
    public static final int EMOJI_DEFAULT = 101;
    public static final int STICKER_COUNT = getStickerCount();
    public static final int STICKER_DEFAULT = 201;
    public static final String STICKER_NAME = "sticker";
    public static final String EMOJI_NAME = "emoji";

    private static final List<EmojiBean> mEmojiBeans = new ArrayList<>();

    public static List<EmojiBean> getEmojiBeans(Context context) {
        if (!mEmojiBeans.isEmpty()) {
            return mEmojiBeans;
        }
        for (int i = 0; i < EMOJI_COUNT; i++) {
            final int resId = context.getResources().getIdentifier(EMOJI_NAME + (i + 1), "drawable", context.getPackageName());
            mEmojiBeans.add(new EmojiBean(resId, EMOJI_DEFAULT + i));
        }
        for (int i = 0; i < STICKER_COUNT; i++) {
            final int resId = context.getResources().getIdentifier(STICKER_NAME + (i + 1), "drawable", context.getPackageName());
            mEmojiBeans.add(new EmojiBean(resId, STICKER_DEFAULT + i));
        }

        return mEmojiBeans;
    }

    //获取表情数量
    public static int getEmojiCount() {
        return 54;
    }

    //获取贴纸数量
    public static int getStickerCount() {
        return 16;
    }

    public static SpannableString spannableString(Context context, String msg) {
        if (!msg.endsWith(",")) {
            msg = msg + ",";
        }
        SpannableString sps = new SpannableString(msg);
        Matcher mat = EMOTICON.matcher(msg);
        while (mat.find()) {
            String exp = mat.group().replace(",", "");
            int expression = Integer.parseInt(exp);
            EmojiBean emojiBean = new EmojiBean(0, expression);
            final int index = getEmojiBeans(context).indexOf(emojiBean);
            if (index < 0) {
                continue;
            }
            final int resourceId = mEmojiBeans.get(index).getResourceId();
            if (resourceId == 0) {
                return sps;
            }
            Drawable drawable = ResourcesCompat.getDrawable(context.getResources(),resourceId,null);
            if (drawable != null) {
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                VerticalImageSpan span = new VerticalImageSpan(drawable);
                int start = mat.start();
                int end = mat.end();
                sps.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        return sps;
    }
}
